package com.hyd.manage.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hyd.common.annotation.Log;
import com.hyd.common.core.controller.BaseController;
import com.hyd.common.core.domain.AjaxResult;
import com.hyd.common.enums.BusinessType;
import com.hyd.manage.domain.StockAudit;
import com.hyd.manage.service.IStockAuditService;
import com.hyd.common.utils.poi.ExcelUtil;
    import com.hyd.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 库存盘点Controller
 *
 * @author hwb
 * @date 2025-02-25
 */
@Api(tags = "库存盘点Controller")
@RestController
@RequestMapping("/manage/audit")
@Slf4j
public class StockAuditController extends BaseController
{
    @Autowired
    private IStockAuditService stockAuditService;

    /**
     * 查询库存盘点列表
     */
    @ApiOperation("查询库存盘点列表")
    @PreAuthorize("@ss.hasPermi('manage:audit:list')")
    @GetMapping("/list")
    public TableDataInfo list(StockAudit stockAudit)
    {

        List<StockAudit> lists = stockAuditService.selectStockAuditList(stockAudit);
        // 去除药品编码一样的数据
        for (int i = 0; i < lists.size(); i++) {
            StockAudit stockAudit1 = lists.get(i);
            for (int j = i + 1; j < lists.size(); j++) {
                StockAudit stockAudit2 = lists.get(j);
                if (stockAudit1.getMedicineCode().equals(stockAudit2.getMedicineCode()) && stockAudit1.getAuditId().equals(stockAudit2.getAuditId())) {
                    // 合并库存数据
                    stockAudit1.setStockQuantity(stockAudit1.getStockQuantity() + stockAudit2.getStockQuantity());
                    // 合并盘点数据
                    stockAudit1.setExpectedCount(stockAudit1.getExpectedCount() + stockAudit2.getExpectedCount());
                    // 合并错误数据
                    stockAudit1.setErrorCount(stockAudit1.getErrorCount() + stockAudit2.getErrorCount());
                    // 从list集合中去除掉重复的数据
                    lists.remove(j);
                }
            }
        }
        startPage();
        return getDataTable(lists);
    }

    /**
     * 导出库存盘点列表
     */
    @ApiOperation("导出库存盘点列表")
    @PreAuthorize("@ss.hasPermi('manage:audit:export')")
    @Log(title = "库存盘点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StockAudit stockAudit)
    {
        List<StockAudit> list = stockAuditService.selectStockAuditList(stockAudit);
        ExcelUtil<StockAudit> util = new ExcelUtil<StockAudit>(StockAudit.class);
        util.exportExcel(response, list, "库存盘点数据");
    }

    /**
     * 获取库存盘点详细信息
     */
    @ApiOperation("获取库存盘点详细信息")
    @PreAuthorize("@ss.hasPermi('manage:audit:query')")
    @GetMapping(value = "/{auditId}")
    public TableDataInfo getInfo(@PathVariable("auditId") Long auditId)
    {
        startPage();
        List<StockAudit> list = stockAuditService.selectStockAuditById(auditId);
        return getDataTable(list);
    }

    /**
     * 新增库存盘点
     */
    @ApiOperation("新增库存盘点")
    @PreAuthorize("@ss.hasPermi('manage:audit:add')")
    @Log(title = "库存盘点", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody List<StockAudit> stockAudits)
    {
        //获取库存盘点表中的盘点记录id的最大值
        List<StockAudit> lists = stockAuditService.selectStockAuditList(null);
        Long auditId;
        //判断lists是否为空
        if (lists.size() == 0) {
            auditId = 1L;
        } else {
            auditId = lists.get(0).getAuditId() + 1;
        }
        log.info("auditId:{}", auditId);
        for (StockAudit stockAudit : stockAudits) {
            stockAudit.setAuditId(auditId);
            stockAuditService.insertStockAudit(stockAudit);
        }
        return AjaxResult.success();
    }

    /**
     * 修改库存盘点
     */
    @ApiOperation("修改库存盘点")
    @PreAuthorize("@ss.hasPermi('manage:audit:edit')")
    @Log(title = "库存盘点", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StockAudit stockAudit)
    {
        return toAjax(stockAuditService.updateStockAudit(stockAudit));
    }

    /**
     * 删除库存盘点
     */
    @ApiOperation("删除库存盘点")
    @PreAuthorize("@ss.hasPermi('manage:audit:remove')")
    @Log(title = "库存盘点", businessType = BusinessType.DELETE)
    @DeleteMapping("/{auditId}")
    public AjaxResult remove(@PathVariable Long[] auditId)
    {
        return toAjax(stockAuditService.deleteStockAuditByIds(auditId));
    }
}